package javaer.starter.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.List;

/**
 * @author yangzhen
 * @date 2021/12/1 16:41
 * @since 1.0.0
 */
public final class Dom4jHelper {
    /**
     * 使用的查询子孙节点的路径
     */
    private static final String SELECT_PATH = "//";
    /**
     * 默认编码
     */
    private static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * default constructor
     */
    private Dom4jHelper() {

    }

    public static Document initDocument(String content) throws DocumentException {
        return DocumentHelper.parseText(content);
    }

    /**
     * @param document
     * @return
     */
    public static Element getRootElement(Document document) {
        return document.getRootElement();
    }

    /**
     * @param element
     * @param path
     * @return
     */
    public static List<Element> getNodes(Element element, String path) {
        return element.elements(path);
    }

    /**
     * @param element
     * @param path
     * @return
     */
    public static Element getSingleNode(Element element, String path) {
        return element.element(path);
    }

    public static void setText(Element element, String text) {
        element.setText(text);
    }

    public static String asXML(Document document) {
        return document.asXML();
    }

    public static Element getSingleNodeOrNew(Element element, String path) {
        Element e = element.element(path);
        if (e == null) {
            e = element.addElement(path);
        }
        return e;
    }

    public static Element addElement(Element element, String path) {
        return element.addElement(path);
    }

    public static void main(String[] args) throws DocumentException {
        String pom = getPom();
        Document document = Dom4jHelper.initDocument(pom);
        Element rootElement = Dom4jHelper.getRootElement(document);
        Element parent = Dom4jHelper.getSingleNode(rootElement, "parent");
        Element groupId = Dom4jHelper.getSingleNode(parent, "groupId");
        Element artifactId = Dom4jHelper.getSingleNode(parent, "artifactId");
        Element version = Dom4jHelper.getSingleNode(parent, "version");
        setText(groupId, "sad");
        setText(artifactId, "sad");
        setText(version, "sad");
        System.out.println(document.asXML());
    }

    public static void test() throws DocumentException {
        String pom = getPom();
        Document document = Dom4jHelper.initDocument(pom);
        Element rootElement = Dom4jHelper.getRootElement(document);
        Element dependencies = Dom4jHelper.getSingleNode(rootElement, "dependencies");
        if (dependencies != null) {
            List<Element> dependencyList = Dom4jHelper.getNodes(dependencies, "dependency");
            if (dependencyList != null) {
                for (Element element : dependencyList) {
                    Element groupId = Dom4jHelper.getSingleNode(element, "groupId");
                    Element artifactId = Dom4jHelper.getSingleNode(element, "artifactId");
                    Element version = Dom4jHelper.getSingleNode(element, "version");
                    System.out.println(groupId.getTextTrim());
                    System.out.println(artifactId.getTextTrim());
                    if (version != null) {
                        System.out.println(version.getTextTrim());
                    }
                }
            }
        }
    }

    public static String getPom() {
        String pom = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
                "    <modelVersion>4.0.0</modelVersion>\n" +
                "\n" +
                "    <parent>\n" +
                "        <groupId>com.tuya.isv</groupId>\n" +
                "        <artifactId>ka-databoard</artifactId>\n" +
                "        <version>${revision}</version>\n" +
                "    </parent>\n" +
                "\n" +
                "    <artifactId>ka-databoard-starter</artifactId>\n" +
                "    <name>ka-databoard-starter</name>\n" +
                "\n" +
                "    <properties>\n" +
                "        <kotlinx-coroutines-core.version>1.3.3</kotlinx-coroutines-core.version>\n" +
                "        <java.version>11</java.version>\n" +
                "    </properties>\n" +
                "\n" +
                "    <dependencies>\n" +
                "\n" +
                "        <dependency>\n" +
                "            <groupId>org.jetbrains.kotlin</groupId>\n" +
                "            <artifactId>kotlin-stdlib</artifactId>\n" +
                "            <version>${kotlin.version}</version>\n" +
                "        </dependency>\n" +
                "\n" +
                "        <dependency>\n" +
                "            <groupId>org.mapstruct</groupId>\n" +
                "            <artifactId>mapstruct</artifactId>\n" +
                "            <version>1.3.1.Final</version>\n" +
                "        </dependency>\n" +
                "\n" +
                "        <dependency>\n" +
                "            <groupId>javax.validation</groupId>\n" +
                "            <artifactId>validation-api</artifactId>\n" +
                "        </dependency>\n" +
                "    </dependencies>\n" +
                "\n" +
                "</project>\n";
        return pom;
    }


}